package org.sakai.service;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.sakai.entity.*;
import org.sakai.entity.dto.ConsultRequestDto;
import org.sakai.entity.vo.ConsultVO;
import org.sakai.mapper.ConsultMapper;
import org.sakai.mapper.DoctorMapper;
import org.sakai.mapper.PatientMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
@CacheConfig(cacheNames = "patient_list")
public class PatientService {
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    ConsultMapper consultMapper;

    @Autowired
    private DoctorMapper doctorMapper;

    public Result<List<Patient>> getPatientList(String userId){

        List<Patient> patientList = patientMapper.getPatientList(userId);
        return Result.success(patientList);
    }

    public Result<Patient> getPatientInfo(String userId, String patientId) {
        // 参数校验
        if (userId == null || userId.trim().isEmpty()) {
            return Result.error(ErrorCode.USER_NOT_FOUND_ERROR);
        }

        // 查询患者信息
        Patient patient = patientMapper.selectByUserIdAndPatientId(userId, patientId);

        // 处理查询结果为空的情况
        if (patient == null) {
            return Result.error(ErrorCode.USER_NOT_FOUND_ERROR);
        }

        return Result.success(patient);
    }


    // ... existing code ...
    public Result<String> addPatient(Patient patient){
        patient.setId(UUID.randomUUID().toString());
        int result = patientMapper.insert(patient);
        if(result > 0){
            return Result.success("添加成功");
        }else{
            return Result.error(ErrorCode.INSERT_ERROR);
        }
    }

    public Result<String> updatePatient(Patient patient){
        int result = patientMapper.updateByPrimaryKeySelective(patient);
        if(result > 0){
            return Result.success("更新成功");
        }else{
            return Result.error(ErrorCode.UPDATE_ERROR);
        }
    }

    public Result<String> delPatient(String patientId){
        int result = patientMapper.deleteByPrimaryKey(patientId);
        if(result > 0){
            return Result.success("删除成功");
        }else{
            return Result.error(ErrorCode.DELETE_ERROR);
        }
    }

    public Result<List<ConsultVO>> getConsultOrderListByPatientId(String uid ,Integer current, Integer pageSize) {
        ConsultRequestDto consultRequestDto = new ConsultRequestDto();
        consultRequestDto.setUserId(uid);
        consultRequestDto.setCurrent(current);
        consultRequestDto.setPageSize(pageSize);
        consultRequestDto.dealPage(consultRequestDto.getCurrent(),consultRequestDto.getPageSize());
        List<ConsultVO> consultVoList = consultMapper.getConsultOrderListByUserId(consultRequestDto);
        for(ConsultVO consultvo : consultVoList){
            consultvo.setDocInfo(doctorMapper.selectByPrimaryKey(consultvo.getDoctorId()));
            consultvo.setPatientInfo(patientMapper.selectByPrimaryKey(consultvo.getPatientId()));
        }
        return Result.success(consultVoList);
    }

    public Result<ConsultVO> getConsultOrderDetailByOrderId(String orderId) {
        Consult consult = consultMapper.selectByPrimaryKey(orderId);
        ConsultVO consultVo = new ConsultVO();
        BeanUtils.copyProperties(consult, consultVo);
        Patient patient = patientMapper.selectByPrimaryKey(consultVo.getPatientId());
        consultVo.setPatientInfo(patient);
        Doctor doctor = doctorMapper.selectByPrimaryKey(consultVo.getDoctorId());
        consultVo.setDocInfo(doctor);
        return Result.success(consultVo);
    }
}
